﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data;

namespace DAO
{
    public class RapChieuDAO
    {
        public static int ThemRapChieu(RapChieu r)
        {
            int kq;
            SqlConnection cn;
            cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_ThemRapChieu";
            cmd.Connection = cn;

            SqlParameter para;

            para = new SqlParameter("@TenRap", SqlDbType.NVarChar, 100);
            para.Direction = ParameterDirection.Input;
            para.Value = r.TenRap;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@DiaChi", SqlDbType.NVarChar, 255);
            para.Direction = ParameterDirection.Input;
            para.Value = r.DiaChi;
            cmd.Parameters.Add(para);

            para = new SqlParameter("kq", SqlDbType.Int);
            para.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(para);

            cmd.ExecuteNonQuery();
            kq = (int)cmd.Parameters["kq"].Value;
            cn.Close();

            return kq;
        }

        public static List<RapChieu> LayDSRapChieu()
        {
            List<RapChieu> dsRap = new List<RapChieu>();
            SqlConnection cn;
            cn = DataProvider.ConnectionData();
            string strSQL;
            strSQL = "Select * From RapChieu";
            SqlCommand cmd = new SqlCommand(strSQL, cn);
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                RapChieu rc = new RapChieu();
                rc.MaRap = (String)dr["MaRap"];
                rc.TenRap = (String)dr["TenRap"];
                dsRap.Add(rc);
            }
            dr.Close();
            cn.Close();
            return dsRap;
        }
    }
}
